﻿using System;
using System.Data;
using System.Windows.Forms;

namespace idataplatform
{
    public partial class userdetail : Form
    {
        private string username;
        private bool remark = false;
        public userdetail(string strvalue)
        {
            InitializeComponent();
            username = strvalue;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "保 存")
            {
                if (textBox1.Text == "")
                {
                    label4.Text = "请输入用户名";
                    label4.Visible = true;
                }
                else if (textBox2.Text == "")
                {
                    label5.Visible = true;
                }
                else
                {
                    if (remark == false)
                    {
                        conncls conns = new conncls();
                        string insSQL = "INSERT INTO users_ipark(uname,upassword,ustatus) VALUES('" + textBox1.Text + "','" + textBox2.Text + "',1)";
                        string selSQL = "SELECT uname FROM users_ipark WHERE uname='" + textBox1.Text + "';";

                        if (conns.rescount(selSQL) != 0)
                        {
                            label4.Text = "用户名已存在！";
                            label4.Visible = true;
                        }
                        else
                        {
                            conns.sqlexec(insSQL);
                            this.DialogResult = DialogResult.OK;
                            this.Dispose();
                            this.Close();
                        }
                    }
                    else
                    {
                        int utype;
                        if (comboBox1.Text == "有效")
                        {
                            utype = 1;
                        }
                        else
                        {
                            utype = 0;
                        }

                        conncls conns = new conncls();
                        string upSQL = "UPDATE users_ipark SET upassword='" + textBox2.Text + "' , ustatus= " + utype + " WHERE uname='" + textBox1.Text + "';";

                        conns.sqlexec(upSQL);
                        this.DialogResult = DialogResult.OK;
                        this.Dispose();
                        this.Close();
                    }
                }
            }
            else if (button1.Text == "修 改")
            {
                textBox2.Enabled = true;
                comboBox1.Enabled = true;
                button1.Text = "保 存";
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            label4.Visible = false;
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            label5.Visible = false;
        }

        private void userdetail_Load(object sender, EventArgs e)
        {
            if (username != "新建用户")
            {
                label3.Visible = true;
                comboBox1.Visible = true;
                button1.Text = "修 改";
                conncls conns = new conncls();
                DataTable dt = new DataTable();
                string selSQL = "SELECT uname,upassword,CASE ustatus WHEN 1 THEN '正常' ELSE '失效' END AS ustatus FROM users_ipark WHERE uname='" + username + "';";
                dt = conns.Dts(selSQL);
                textBox1.Text = dt.Rows[0][0].ToString();
                textBox2.Text = dt.Rows[0][1].ToString();
                comboBox1.Text = dt.Rows[0][2].ToString();
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                comboBox1.Enabled = false;
                remark = true;
            }
        }
    }
}
